package com.djk.utils;

import com.djk.enums.ResultCode;
import lombok.Data;

@Data
public class Result<T> {
    private int status;//状态码 0 成功，负数 失败
    private String message;//状态描述信息
    private T data;//返回的数据

    public Result() {
    }

    public Result(int status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static Result baseResult(ResultCode resultCode) {
        Integer code = resultCode.getCode();
        String msg = resultCode.getMessage();
        return new Result(code, msg, null);
    }

    public static Result ok() {
        return baseResult(ResultCode.SUCCESS);
    }

    public static <T> Result ok(T t) {
        Result result = baseResult(ResultCode.SUCCESS);
        result.setData(t);
        return result;
    }

    public static Result error() {
        return baseResult(ResultCode.FAILURE);
    }

    public static Result userNotLoggedIn() {
        return baseResult(ResultCode.USER_NOT_LOGGED_IN);
    }

    public static <T> Result error(T t) {
        Result result = baseResult(ResultCode.FAILURE);
        result.setData(t);
        return result;
    }

    public static Result usernameError() {
        Result result = baseResult(ResultCode.USERNAME_NOT_FOUND);
        return result;
    }

    public static Result passwordError() {
        Result result = baseResult(ResultCode.PASSWORD_INCORRECT);
        return result;
    }

    public static Result usernameOrPasswordError() {
        Result result = baseResult(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        return result;
    }

    public static Result audioTypeError() {
        return baseResult(ResultCode.AUDIO_TYPE_ERROR);
    }

}
